#!/usr/bin/env python
from ChessView import *
from ChessBoard.ChessBoard import ChessBoard
import wx

class BasicController(ChessView):
    list = None
    def __init__(self, frame, size, list=None):
        ChessView.__init__(self, frame, size)
        self.model = ChessBoard()
        self.Bind(EVT_PIECE_MOVED, self.OnPieceMoved)
        if list: self.list = list
##        fen = 'r3k2r/1pp1ppP1/8/8/8/8/1PP1PPp1/R3K2R w KQkq - 0 1'
##        self.model.setFEN(fen)
##        self.setupFromFEN(fen)
       
    def OnPieceMoved(self,event):       
        if self.model.addTextMove(event.move):
            self.MoveWasValid(self.model.getLastMoveType())
            if self.list: self.list.publishMove(self.model.getLastTextMove())
            return True
        else:
            if self.model.getReason() == ChessBoard.MUST_SET_PROMOTION:
                pieces = 'qrbn'
                if self.model.getTurn() == ChessBoard.WHITE: pieces = pieces.upper()
                dialog = PromotionDialog(self, self.pieces, pieces)
                self.promotion = dialog.ShowModal()
                dialog.Destroy()
                
                setpieces = {'q':ChessBoard.QUEEN, 'r':ChessBoard.ROOK, 'b':ChessBoard.BISHOP, 'n':ChessBoard.KNIGHT}
                self.model.setPromotion(setpieces[self.promotion.lower()])                
                self.model.addTextMove(event.move)
                self.MoveWasValid(self.model.getLastMoveType())
                if self.list: self.list.publishMove(self.model.getLastTextMove())
                return True
            else:                
                self.MoveWasInvalid()
                return False

class MultiPlayerController(BasicController):
    myColor = ChessBoard.NOCOLOR
    opponent = None
    
    def __init__(self, frame, size, list=None):
        BasicController.__init__(self, frame, size, list)
    
    def setColor(self, myColor):
        self.myColor = myColor

    def setOpponent(self, opponent):
        self.opponent = opponent
    
    def OnPieceMoved(self,event):
        if self.model.getTurn() == self.myColor:
            if BasicController.OnPieceMoved(self, event):
                self.notifyOpponent(self.model.getLastTextMove())
                return True
        else:
            self.MoveWasInvalid()
            return False
    
    def notifyOpponent(self, movetext):
        self.opponent.notify(movetext)
            
    def notify(self, movetext):
        if self.model.getTurn() == self.myColor: return False
        if not self.model.addTextMove(movetext): 
            return False
        movetype = self.model.getLastMoveType()
        if movetype == ChessBoard.PROMOTION_MOVE:
            promote_to = movetext[-1]
            if promote_to in "+#": promote_to = movetext[-2]
            if self.myColor == ChessBoard.WHITE: promote_to = promote_to.lower()
            self.SetPromotion(promote_to)
        self.DoTextMove(self.model.getLastTextMove(ChessBoard.AN), movetype, True)
        if self.list: self.list.publishMove(movetext)
        return True

if __name__ == '__main__':
    app = wx.PySimpleApp()
    size = (520,520)
    frame = wx.Frame(None, wx.ID_ANY, "Chess game test application")
    controller = BasicController(frame, size)

    frame.Fit()  
    frame.Show()
    app.MainLoop()
